package org.jdsm.flow.core.users.impl.jpa;


import org.jdsm.flow.core.users.IUser;

import javax.persistence.*;
import java.util.Set;

/**
 * User: Mykhaylo Komarichyn
 * Date: 11.06.2010
 * Time: 14:21:51
 */
@Entity
@Table(name = "WF_USERS_USER")
@NamedQueries(
        {
                @NamedQuery(name = "user.findUserByName", query = "SELECT u FROM User u WHERE u.name = :userName"),
                @NamedQuery(name = "user.findRoles", query = "SELECT u.roles FROM User u WHERE u.id = :userId "),
                @NamedQuery(name = "user.findGroups", query = "SELECT u.groups FROM User u WHERE u.id = :userId "),
                @NamedQuery(name = "user.findRoleWithMaxPermission", query = "SELECT MIN(r.permission) FROM Role r INNER JOIN r.users u WHERE u.id = :userId"),
                @NamedQuery(name = "user.findUserByNickName", query = "SELECT u FROM User u WHERE u.nickName = :nickName")

        }
)
public class User implements IUser<Long> {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "ID")
    private Long id;
    @Column(name = "NAME", length = 255, nullable = false, unique = true)
    private String name;
    @Column(name = "NICK_NAME", length = 255, nullable = false)
    private String nickName;
    @Column(name = "PASSWORD", length = 255, nullable = false)
    private String password;
    @Column(name = "HINT", length = 255)
    private String hint;


    @ManyToMany(targetEntity = Group.class)
    @JoinTable(name = "WF_TBL_USER_GROUP",
            joinColumns = @JoinColumn(name = "UID", referencedColumnName = "ID"),
            inverseJoinColumns = @JoinColumn(name = "GID", referencedColumnName = "ID"))
    private Set<Group> groups;


    @ManyToMany(cascade = CascadeType.REFRESH, targetEntity = Role.class)
    @JoinTable(name = "WF_TBL_USER_ROLE",
            joinColumns = @JoinColumn(name = "UID", referencedColumnName = "ID"),
            inverseJoinColumns = @JoinColumn(name = "RID", referencedColumnName = "ID"))
    private Set<Role> roles;

    public Long getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getNickName() {
        return nickName;
    }

    public String getPassword() {
        return password;
    }

    public String getHint() {
        return hint;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setNickName(String nickName) {
        this.nickName = nickName;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setHint(String hint) {
        this.hint = hint;
    }


    public Set<Group> getGroups() {
        return groups;
    }

    public void setGroups(Set<Group> groups) {
        this.groups = groups;
    }

    public Set<Role> getRoles() {
        return roles;
    }

    public void setRoles(Set<Role> roles) {
        this.roles = roles;
    }

    @Override    
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        User user = (User) o;


        if (hint != null ? !hint.equals(user.hint) : user.hint != null) return false;
        if (id != null ? !id.equals(user.id) : user.id != null) return false;
        if (name != null ? !name.equals(user.name) : user.name != null) return false;
        if (nickName != null ? !nickName.equals(user.nickName) : user.nickName != null) return false;
        if (password != null ? !password.equals(user.password) : user.password != null) return false;


        return true;
    }

    @Override
    public int hashCode() {
        int result = id != null ? id.hashCode() : 0;
        result = 31 * result + (name != null ? name.hashCode() : 0);
        result = 31 * result + (nickName != null ? nickName.hashCode() : 0);
        result = 31 * result + (password != null ? password.hashCode() : 0);
        result = 31 * result + (hint != null ? hint.hashCode() : 0);
        return result;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder();
        sb.append("User");
        sb.append("{id=").append(id);
        sb.append(", name='").append(name).append('\'');
        sb.append(", nickName='").append(nickName).append('\'');
        sb.append(", password='").append(password).append('\'');
        sb.append(", hint='").append(hint).append('\'');
        sb.append('}');
        return sb.toString();
    }


    public static void main(String[] args) {
        int i, N=20;
        for(i = 0 ; -i < N ; i--)
            System.out.print("*");
    }

}
